package com.wanmait.happyheating.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wanmait.happyheating.pojo.City;
import com.wanmait.happyheating.pojo.District;
import com.wanmait.happyheating.pojo.Province;
import com.wanmait.happyheating.pojo.Street;
import com.wanmait.happyheating.service.CityService;
import com.wanmait.happyheating.service.DistrictService;
import com.wanmait.happyheating.service.ProvinceService;
import com.wanmait.happyheating.service.StreetService;
import com.wanmait.happyheating.util.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@RestController//指定他是一个控制器的controller注解，每个方法默认加上注解@ResponseBody
@RequestMapping("/manage")//公用的路径
public class UserSafeAddressController
{

        @Resource//
        private DistrictService districtService;//链接什么数据库就写什么Service
        @Resource
        private ProvinceService provinceService;
         @Resource
         private CityService cityService;
         @Resource
         private StreetService streetService;

        @PostMapping("showDistrict")//请求方式
        public Result findStreetType(Integer id)//@RequsetBody注解 接收对象
        {
            LambdaQueryWrapper<District>districtLambdaQueryWrapper=new LambdaQueryWrapper<>();
            districtLambdaQueryWrapper.eq(District::getId,id);
            District district=districtService.getById(id);
                districtService.removeById(id);

            return Result.success(district);
        }

    @PostMapping("selectAllProvince")
    public Result selectAllProvince() {
        return Result.success(provinceService.list());
    }

    @PostMapping("findCityByProvinceId")
    public Result findCityByProvinceId(@RequestBody Province province) {
        LambdaQueryWrapper<City> cityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cityLambdaQueryWrapper.eq(City::getProvinceId, province.getId());
        List<City> cityList = cityService.list(cityLambdaQueryWrapper);
        return Result.success(cityList);
    }

    @PostMapping("findDistrictByCityId")
    public Result findDistrictByCityId(@RequestBody City city) {
        LambdaQueryWrapper<District> districtLambdaQueryWrapper = new LambdaQueryWrapper<>();
        districtLambdaQueryWrapper.eq(District::getCityId, city.getId());
        List<District> districtList = districtService.list(districtLambdaQueryWrapper);
        return Result.success(districtList);
    }

    @PostMapping("findStreetByDistrictId")
    public Result findStreetByDistrictId(@RequestBody District district) {
        LambdaQueryWrapper<Street> streetLambdaQueryWrapper = new LambdaQueryWrapper<>();
        streetLambdaQueryWrapper.eq(Street::getDistrictId, district.getId());
        List<Street> districtList = streetService.list(streetLambdaQueryWrapper);
        return Result.success(districtList);
    }

}
